package gold.digger;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC179 {

//    Input
//[824,938,1399,5607,6973,5703,9609,4398,8247]
//    Output
//"9609 938      8247 824         6973     5703 5607 4398 1399"
//    Expected
//"9609 938      824 8247         6973      5703 5607 4398 1399"


    public String largestNumber(int[] arr) {
        String[] arrWrap = new String[arr.length];
        for (int i = 0; i < arr.length; i++) {
            arrWrap[i] = String.valueOf(arr[i]);
        }

        Arrays.sort(arrWrap, new Comparator<String>() {
            public int compare(String a, String b) {
                long aFirstNum = Long.valueOf(a) * (long) (Math.pow(10, b.length())) + Long.valueOf(b);
                long bFirstNum = Long.valueOf(b) * (long) (Math.pow(10, a.length())) + Long.valueOf(a);
                return (int) (bFirstNum - aFirstNum);
            }
        });

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arrWrap.length; i++) {
            sb.append(arrWrap[i]);
        }

        String rawNumber = sb.toString();
        int startZeroPosition = 0;
        for (; startZeroPosition < rawNumber.length(); ) {
            if (rawNumber.charAt(startZeroPosition) == '0') ++startZeroPosition;
            else break;
        }

        String processedNumber = rawNumber.substring(startZeroPosition);
        return processedNumber.length() <= 0 ? "0" : processedNumber;
    }


    public static void main(String[] args) {
        LC179 an = new LC179();
//        int[] arr = {824, 938, 1399, 5607, 6973, 5703, 9609, 4398, 8247};
        int[] arr = {22, 22, 22};
        System.out.println(an.largestNumber(arr));
    }
}
